package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.code.QRcodeImageUtil;
import com.ruoyi.system.domain.TmcEpidemicUser;
import com.ruoyi.system.domain.TmcEpidemicUserRecord;
import com.ruoyi.system.service.impl.TmcUserServiceImpl;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
// 用户行程
@RestController
@RequestMapping("/system/stroke")
public class StrokeController extends BaseController {

    @Autowired
    private TmcUserServiceImpl tmcUserService;

    //根据用户ID查询二维码基本信息
    @GetMapping("/queryUserInfo")
    public AjaxResult queryUserInfo(Long publicuserId) {
        return AjaxResult.success(tmcUserService.queryUserInfo(publicuserId));
    }

    //新增行程信息 不为空注册
    @PostMapping("/insertRecord")
    public AjaxResult insertRecord(@RequestBody TmcEpidemicUserRecord tmcEpidemicUserRecord) {
        //查询当日上报次数 每日最多提交两次
        if(null!=tmcUserService.selectSubmit(tmcEpidemicUserRecord)){
            //查询是否上报过
            TmcEpidemicUserRecord tmcEpidemicUserRecord1 = tmcUserService.selectSubmit(tmcEpidemicUserRecord);
            if (tmcEpidemicUserRecord1.getUpdatecount()==2){
                return AjaxResult.success("今日提交已达上限");
            }else {
                //修改赋值
                tmcEpidemicUserRecord.setId(tmcEpidemicUserRecord1.getId());
                tmcEpidemicUserRecord.setUpdatecount(tmcEpidemicUserRecord1.getUpdatecount()+1);
                tmcUserService.updateRecord(tmcEpidemicUserRecord);
                //返回查询到的Id
                return AjaxResult.success("更新成功",tmcEpidemicUserRecord1.getId());
            }
        }
        return AjaxResult.success("新增成功",tmcUserService.insertRecord(tmcEpidemicUserRecord));
    }

    //新增行程信息
    @PostMapping("/updateRecord")
    public AjaxResult updateRecord(@RequestBody TmcEpidemicUserRecord tmcEpidemicUserRecord) {
        return toAjax(tmcUserService.updateRecord(tmcEpidemicUserRecord));
    }

    //查询用户每日打卡记录
    @GetMapping("/queryUserRecord")
    public AjaxResult queryUserRecord(Long publicuserId) {
        return AjaxResult.success(tmcUserService.queryUserRecord(publicuserId));
    }
    //查询用户每日打卡记录详情
    @GetMapping("/queryUserRecordById")
    public AjaxResult queryUserRecordById(Long id) {
        return AjaxResult.success(tmcUserService.queryUserRecordById(id));
    }
    //查询二维码状态
    @GetMapping("/codeStatus")
    public AjaxResult codeStatus(Long publicuserId) throws Exception {
        //生成二维码
        String filepath = RuoYiConfig.getProfile()+"//"+publicuserId;
        String filename = publicuserId+"";
        Integer color=0;
        //生成一个随机串
        TmcEpidemicUser tmcEpidemicUser = tmcUserService.queryUserInfo(publicuserId);
        //红色  16711680
        //绿色码 3329330
        //橙色 16744192
        if(null!=publicuserId){
        if (tmcEpidemicUser.getState()==1){//判断二维码状态
            color=3329330;
        }else if(tmcEpidemicUser.getState()==2){
            color=16744192;
        }else {
            color=16711680;
        }}
        QRcodeImageUtil.buildQuickMark(tmcEpidemicUser.getQrcode(), filepath, filename,color);
        System.out.println("profile//"+ publicuserId + "//"+filename+".png");
        return AjaxResult.success("生成行程二维码成功","profile/"+ publicuserId + "/"+filename+".png");
    }

    // 上传图片
    @Log(title = "图片", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload")
    @CrossOrigin
    public AjaxResult pictureUpload(MultipartFile file, Long id,Long publicuserId) {
        try {
            String profile = RuoYiConfig.getProfile();
            byte[] bytes = file.getBytes();
            //文件夹
            Path path2 = Paths.get(profile+"//" + id);
            //具体文件
            Path path = Paths.get(profile+"//" + id+ "//" + file.getOriginalFilename());
            Path path1 = Paths.get("profile//" + id + "//" + file.getOriginalFilename());
            File file1 = path.toFile();
            //创建文件夹
            if (!file1.exists()) {
                path2.toFile().mkdirs();
            }
            if (file1.isFile()) {
                String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                String replace = path1.toString().replace(suffixName, "(" + (int) (Math.random() * 10) + ")" + suffixName);
            }
            Files.write(path, bytes);//创建文件
            TmcEpidemicUserRecord tmcEpidemicUserRecord = new TmcEpidemicUserRecord();
            tmcEpidemicUserRecord.setAttachUrl(path1.toString());
            tmcEpidemicUserRecord.setId(id);
            tmcEpidemicUserRecord.setPublicuserId(publicuserId);
            if (tmcUserService.updateRecord(tmcEpidemicUserRecord) > 0) {
                return AjaxResult.success("文件上传成功");
            } else {
                if (file1.isFile()) {
                    file1.delete();
                }
                return AjaxResult.error("文件上传失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("文件上传失败");
        }
    }

}
